package com.cy.store.service.impl;

import com.cy.store.entity.User;
import com.cy.store.mapper.UserMapper;
import com.cy.store.service.IUserService;
import com.cy.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.UUID;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    private String getMD5Password(String password,String salt){
        //MD5加密算法的调用
        return DigestUtils.md5DigestAsHex((salt+password+salt).getBytes()).toUpperCase();

    }

    @Override
    public void reg(User user) {
        String username = user.getUsername();
        User result = userMapper.findByUsername(username);
        if(result != null){
            throw new UsernameDuplicatedException("用户名被占用!");
        }

        String oldPassword = user.getPassword();
        //获取盐值
        String salt = UUID.randomUUID().toString().toUpperCase();
        //密码与盐值加密处理
        String md5Password = getMD5Password(oldPassword,salt);
        user.setPassword(md5Password);
        user.setSalt(salt);

        //补全数据
        user.setIsDelete(0);
        //补全日志数据
        user.setCreatedUser(user.getUsername());
        user.setModifiedUser(user.getUsername());
        Date date = new Date();
        user.setCreatedTime(date);
        user.setModifiedTime(date);

        Integer row = userMapper.insert(user);
        if(row!=1){
            throw new InsertException("在用户插入数据时产生未知异常!");
        }

    }

    @Override
    public User login(String username, String password) {
        User result = userMapper.findByUsername(username);
        //查询用户是否存在，如果不存在则抛出异常
        if(result==null){
            throw new UserNotFoundException("用户数据不存在");
        }
        //检测密码是否正确
        //获取数据库加密后的密码
        String oldPassword = result.getPassword();
        //先获取盐值，上一次注册时所生成的盐值
        String salt = result.getSalt();
        //将用户所传进来的密码进行同样的md5算法加密
        String newMd5Password = getMD5Password(password,salt);
        //将密码进行比较
        if(!oldPassword.equals(newMd5Password)){
            throw new PasswordNotMatchException("用户密码错误");
        }

        //判断is_delete字段值是否为1，表示标记为已删除
        if(result.getIsDelete()==1){
            throw new UserNotFoundException("用户数据不存在");
        }


        return result;


    }

    @Override
    public void changePassword(Integer uid, String username, String oldPassword, String newPassword) {
        User result = userMapper.findByUid(uid);
        if(result==null||result.getIsDelete()==1){
            throw new UserNotFoundException("用户数据不存在");
        }

        //原始密码与数据库中密码比较
        String oldMd5Password = getMD5Password(oldPassword,result.getSalt());
        if(!result.getPassword().equals(oldMd5Password)){
            throw new PasswordNotMatchException("密码错误");
        }
        //将新密码设置为用户密码，并且进行加密
        String newMd5Password = getMD5Password(newPassword,result.getSalt());
        Integer len = userMapper.updatePasswordByUid(uid,newMd5Password,username,new Date());
        if(len != 1){
            throw new UpdateException("更新数据发生未知异常");
        }


    }

    @Override
    public User getByUid(Integer uid) {
        User result = userMapper.findByUid(uid);
        if(result==null||result.getIsDelete()==1){
            throw new UserNotFoundException("用户数据不存在");
        }
        User us = new User();
        us.setUid(uid);
        us.setUsername(result.getUsername());
        us.setPhone(result.getPhone());
        us.setEmail(result.getEmail());
        us.setGender(result.getGender());
        return us;
    }

    @Override
    public void changeInfo(Integer uid, String username, User user) {
        User result = userMapper.findByUid(uid);
        if(result==null||result.getIsDelete()==1){
            throw new UserNotFoundException("用户数据不存在");
        }

        user.setUid(uid);
        user.setUsername(username);
        user.setModifiedTime(new Date());
        user.setModifiedUser(username);

        Integer len = userMapper.updateInfoByUid(user);
        if(len!=1){
            throw new UpdateException("更新时出现未知异常");
        }

    }


    @Override
    public void changeAvatar(Integer uid, String avatar, String username) {
        //查询当前用户是否存在
        User result = userMapper.findByUid(uid);
        if(result==null||result.getIsDelete()==1){
            throw new UserNotFoundException("用户数据不存在");
        }
        Integer len = userMapper.updateAvatarByUid(uid,avatar,username,new Date());
        if(len != 1){
            throw new UpdateException("更新用户头像出现未知异常");
        }
    }
}
